Compressible Map
A hash map that allows compressing the least recently used values. Useful when you need to store a
lot of large values in memory. You can either use the "bincode_lz4" feature to get a default
compression algorithm for serializable values, or define your own compression method for the value
type using the Compressible
and Decompressible
traits.
Example: Single-threaded Compression and Access
fn main() {
let compression_level = 10;
let mut map = CompressibleMap::new_bincode_lz4(compression_level);
for i in 0..100 {
map.insert(i, BigValue::new());
}
for _ in 0..50 {
map.compress_lru();
}
for i in 25..75 {
map.get(i);
}
}
Example: Multi-threaded Decompression and Access
use crossbeam::{channel, thread};
fn main() {
let mut map = CompressibleMap::<_, _, _>::new(());
for i in 0..100 {
map.insert(i, BigValue::new());
}
for _ in 0..50 {
map.compress_lru();
}
let map_ref = ↦
let (tx, rx) = channel::unbounded();
{
let mut txs = Vec::new();
for _ in 0..99 {
txs.push(tx.clone());
}
txs.push(tx);
let txs_ref = &txs;
thread::scope(|s| {
for i in 0..100 {
s.spawn(move |_| {
let local_cache = LocalCache::new();
let big_value = map_ref.get_const(i, &local_cache);
txs_ref[i as usize].send(local_cache).unwrap();
});
}
})
.unwrap();
}
loop {
match rx.recv() {
Ok(cache) => map.flush_local_cache(cache),
Err(_) => {
break;
}
}
}
assert_eq!(map.len_cached(), 100);
}